package br.com.biblioteca.dao;

import org.apache.log4j.Logger;
import org.hibernate.Session;

/**
 *
 * @author João Sávio Ceregatti Longo - joaosavio@gmail.com
 */
public class HibernateDAOFactory extends DAOFactory {
    private static Logger LOG = Logger.getLogger(HibernateDAOFactory.class);
  
    private GenericHibernateDAO instantiateDAO(Class daoClass) {
        try {
            GenericHibernateDAO genericHibernateDAO = (GenericHibernateDAO)daoClass.newInstance();
            genericHibernateDAO.setSession(getCurrentSession());
            return genericHibernateDAO;
        } catch (Exception e) {
            LOG.fatal("Can not instantiate DAO: " + daoClass + ".", e);
            throw new RuntimeException("Can not instantiate DAO: " + daoClass, e);
        }
    }
 
    // You could override this if you don't want HibernateUtil for lookup
    protected Session getCurrentSession() {
        return HibernateUtil.getSession();
    }

    @Override
    public IUserDAO getUserDAO() {
        return (IUserDAO)instantiateDAO(UserDAO.class);
    }

    @Override
    public IBookDAO getBookDAO() {
        return (IBookDAO)instantiateDAO(BookDAO.class);
    }

    @Override
    public ILoanDAO getLoanDAO() {
        return (ILoanDAO)instantiateDAO(LoanDAO.class);
    }
   
}
